﻿using System;
using System.Diagnostics;
using System.Threading;
using System.Xml.Linq;
using Extensions;
using System.Linq;

namespace ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Demo demo = new Demo();
            // delegate usage
            //demo.Test(Console.ReadLine());

            // event usage
            //demo.Play();

            // action
            //Action<string> action = demo.Test;
            //action(Console.ReadLine());
            //Action<string> action = (testName) => Console.WriteLine($"the delegate method run time is {testName} ms");

            //action(Console.ReadLine());


            //func
            //Func<string, long> func = demo.DemoA;
            //var result = func(Console.ReadLine());
            //Console.WriteLine(result);

            Func<string, string, bool> func = (x, y) => char.IsUpper(x[0]);
            Console.WriteLine(func(Console.ReadLine(), ""));

            //var value = Console.ReadLine();
            //var result = demo.FirstCharUpper(value);
            //Console.WriteLine(result);

            //result = value.FirstCharUpper();
            //Console.WriteLine(result);

            var result = "".Where((charCurrent) => char.IsUpper(charCurrent))
                              .Take(3)
                              .Take(4)
                              .ToArray();

            //Console.WriteLine(result);


            Console.WriteLine("Hello World!");
        }
    }

    class Demo
    {
        private delegate long DelegateDemo(string name);
        private readonly DelegateDemo _demo = null;

        private event EventHandler<EventArgs> _event;

        public Demo()
        {
            _demo = DemoA;
            _event += EventTest;
        }

        public long DemoA(string name)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            Console.WriteLine($"this is {name} delegate demo runnig");
            Thread.Sleep(500);
            stopwatch.Stop();
            return stopwatch.ElapsedMilliseconds;
        }

        public void Test(string testName)
        {
            var result = _demo(testName);
            Console.WriteLine($"the delegate method run time is {result} ms");
        }

        private void EventTest(object sender, EventArgs args)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            Console.WriteLine($"this is event demo runnig");
            Thread.Sleep(500);
            stopwatch.Stop();
            var result = stopwatch.ElapsedMilliseconds;
            Console.WriteLine($"the event method run time is {result} ms");
        }

        public void Play() => _event.Invoke(this, new EventArgs());

        public string FirstCharUpper(string value) => char.ToUpper(value[0]) + value[1..^2];
    }
}
